<template>
	<view>
		<cu-custom :bgColor="'bg-' + theme.backgroundColor" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">直播和用户数据</block>
		</cu-custom>
		<view>
			<zbTable :show-header="true" :columns="column" :stripe="true" :fit="false" :border="true"
				@row-click="rowClick" @search="changeSearch" :page="page" :data="data" @export="handExport"
				@changePage="changePage" :loading="loading"></zbTable>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	import util from "utils/util";
	import api from "utils/api";
	import config from "@/config/env.js";
	import zbTable from "components/zb-table/zb-table.vue";
	export default {
		components: {
			zbTable,
		},
		data() {
			return {
				theme: app.globalData.theme, //全局颜色变量
				page: {
					current: 1,
					size: 10,
					total: 0,
				},
				column: [{
						label: "#",
						type: "index",
						width: 40,
					},
					{
						label: "直播间名称",
						name: "liveName",
						overHidden: true,
						search: true,
					}, {
						label: "直播间号",
						name: "liveNo",
						overHidden: true,
						search: true,
					},
					{
						label: "直播开始时间",
						name: "startTime",
						overHidden: true,
					}, {
						label: "直播结束时间",
						name: "endTime",
						overHidden: true,
					}, {
						label: "直播观看总人数",
						name: "totalViewer",
						overHidden: true,
					},
					{
						label: "直播中用户昵称",
						name: "nickName",
						overHidden: true,
					}, {
						label: "用户id",
						name: "userId",
						overHidden: true,
					},
					{
						label: "用户手机号",
						name: "phone",
						overHidden: true,
					},
					{
						label: "用户手机号",
						name: "userPhone",
						hide: true,
						search: true,
					}, {
						label: "首次进入直播时间",
						name: "firstTime",
						overHidden: true,
					}, {
						label: "最后退出直播时间",
						name: "finalTime",
						overHidden: true,
					}, {
						label: "观看直播总时间",
						name: "viewTime",
						overHidden: true,
					},
					{
						label: "用户答题结果",
						name: "answerFlag",
						// 1对0错
						filters: {
							0: "错",
							1: "对"
						},
					},
					{
						label: "获取积分数",
						name: "receiveIntegral",
						overHidden: true,
					}, {
						label: "获取红包金额",
						name: "receiveRedPackMoney",
						overHidden: true,
					},
					{
						label: "红包状态",
						name: "checkFlag",
						filters: {
							0: "未打款",
							1: "已打款",
							2: "无需核销"
						},
					},
					// {
					// 	label: "用户答题结果",
					// 	name: "activityInfo",
					// 	overHidden: true,
					// },

					{
						label: "用户抽奖结果",
						name: "lotteryName",
						overHidden: true,
					}, {
						label: "用户优惠券结果",
						name: "couponName",
						overHidden: true,
					},

				],
				data: [],
				searchform: {},
				loading: false,
			};
		},
		onLoad() {
			this.getData();
		},
		onShow() {},
		//方法
		methods: {
			rowClick() {},
			changeSearch(form) {
				this.searchform = form;
				this.page.current = 1;
				this.getData();
			},
			changePage(e) {
				this.page.current = e.current;
				this.data = []
				this.getData();
			},
			handExport() {
				console.log('导出');
				uni.showLoading({
					title: "加载中",
					mask: true,
				});
				let that = this;
				let search = "?"
				for (let key in this.searchform) {
					search += key + "=" + this.searchform[key] + "&"
				}
				//删除最后一个&
				search = search.substring(0, search.length - 1)
				uni.downloadFile({
					url: config.basePath + "/liveapi/excel/export/liveAndUserData" + search,
					header: {
						"client-type": "MA", //客户端类型小程序
						"app-id": uni.getAccountInfoSync().miniProgram.appId, //小程序appId
						"third-session": uni.getStorageSync("third_session") ?
							uni.getStorageSync("third_session") : "",
					},
					success: (file) => {
						let fileManagerObj = uni.getFileSystemManager();
						let FileName = decodeURIComponent(file.header.FileName);
						const filePath = `${wx.env.USER_DATA_PATH}/${FileName}`;
						console.log(filePath, "filePath");
						fileManagerObj.saveFile({
							tempFilePath: file.tempFilePath,
							filePath: filePath,
							success: (res) => {
								uni.hideLoading();
								uni.showModal({
									title: "提示",
									content: "ios:点击预览页面右上角三个点-->用其他应用打开-->存储到文件\n" +
										"安卓:点击预览页面右上角三个点-->保存到手机",
									showCancel: false,
									success: ({
										confirm,
										cancel
									}) => {
										if (confirm) {
											var filePath = res.savedFilePath;
											uni.openDocument({
												//新开页面打开文档，支持格式：doc, xls, ppt, pdf, docx, xlsx, pptx。
												filePath: filePath,
												showMenu: true,
												success: function(res) {
													console.log("打开文档成功");
												},
											});
										}
									},
									fail: (error) => {
										uni.hideLoading();
										uni.showToast({
											title: "导出失败",
											icon: "none",
										});
									},
								});
							},
						});
					},
					fail: (error) => {
						uni.hideLoading();
						uni.showToast({
							title: "导出失败",
							icon: "none",
						});
					},
				});
			},
			getData(form) {
				this.loading = true
				let para = Object.assign(this.page, this.searchform);
				api.liveAndUserDataPage(util.filterForm(para)).then((res) => {
					this.data = res.data.records;
					this.page.total = res.data.total;
					this.loading = false
					// if (pointsRecord.length < this.page.size) {
					// 	this.loadmore = false;
					// }
				}).catch(err => {
					this.loading = false
				})
			},
		},
	};
</script>

<style lang="scss" scoped></style>